package praktikumid.w11.p4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class UndoHistory {
	/**
	 * Simple element, which technically only has
	 * one String - name. 
	 * @author Ago
	 *
	 */
	static class Element {
		/**
		 * Name of the element.
		 */
		String name;
		
		/**
		 * Constructor, where we set the name.
		 * @param name
		 */
		public Element(String name) {
			this.name = name;
		}
		
		public Element(Element copy) {
			name = copy.name;
		}
		
		@Override
		public String toString() {
			// let's have a "nice" printout
			return name;
		}
	}
	
	/**
	 * A list of elements.
	 */
	static List<Element> elements = new ArrayList<Element>();

	static class HistoryState {
		List<Element> elements = new ArrayList<Element>();
	}
	
	static List<HistoryState> history = new ArrayList<HistoryState>();
	static int historyIndex = -1;
	
	/**
	 * Main.
	 * @param args arguments.
	 * @throws IOException Dealing with input can throw exception.
	 */
	public static void main(String[] args) throws IOException {
		takeSnapshot();
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		while(true) {
			printHelp();
			String line = br.readLine();
			// we expect correct input
			// just split everything by space
			String[] tokens = line.split(" ");
			int op = Integer.valueOf(tokens[0]);
			int elid = -1;
			switch (op) {
			case 1: // add element
				elements.add(new Element(tokens[1]));
				break;
			case 2: // modify element, rename
				elid = Integer.valueOf(tokens[1]);
				Element e = elements.get(elid);
				e.name = tokens[2];
				break;
			case 3: // delete element
				elid = Integer.valueOf(tokens[1]);
				elements.remove(elid);
				break;
			case 4: // printout
				printElements();
				break;
			case 5:
				undo();
				printElements();
				break;
			case 0:
				System.exit(0);
			}

			if (op > 0 && op < 4) {
				takeSnapshot();
			}
			
		}
		
	}
	
	public static void takeSnapshot() {
		HistoryState state = new HistoryState();
		for (Element e : elements) {
			state.elements.add(new Element(e));
		}
		historyIndex++;
		while (historyIndex < history.size()) {
			history.remove(historyIndex);
		}
		history.add(state);
	}
	
	public static void undo() {
		historyIndex--;
		HistoryState s = history.get(historyIndex);
		// copy to elements
		elements = new ArrayList<Element>();
		for (Element e : s.elements) {
			elements.add(new Element(e));
		}
	}
	
	/**
	 * Prints out the elements in the list.
	 */
	public static void printElements() {
		System.out.println("Elements:");
		for (int i = 0; i < elements.size(); i++) {
			System.out.println(i + ". " + elements.get(i));
		}
	}
	
	/**
	 * Prints out help - what can be done with the program.
	 */
	public static void printHelp() {
		System.out.println("  1 add element [1 name]");
		System.out.println("  2 change element [2 elid name]");
		System.out.println("  3 delete element [3 elid]");
		System.out.println("  4 list elements [4]");
		System.out.println("  5 undo [5]");
		System.out.println("  0 exit [0]");
	}
}

